using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Common.BL;

public partial class admin_Default : System.Web.UI.Page
{
    public int _TopMenuItem = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.EnableViewState = true;

        LanguageUtility.CheckLanguage(Request, Response, Session);

        if (!CheckAuthentication())
        {
            _TopMenuItem = 11;

            HtmlGenericControl divTip = new HtmlGenericControl("div");
            divTip.Attributes.Add("class", "tip");
            divTip.InnerHtml = LanguageUtility.GetLanguageText("NOT_AUTHORISE", Session)
                + "&nbsp;<a href=\"../admin/\">" + LanguageUtility.GetLanguageText("CLICK_RETURN", Session) + "</a>";
            this.main.Controls.Add(divTip);
            return;
        }

        string controlPath = string.Empty;
        string type = Request.QueryString["type"];
        if (type != null)
        {
            if (type.Equals("user", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 3;
                controlPath = "webparts/AdminUser/AdminUserManager.ascx";
            }
            else if (type.Equals("lang", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 5;
                controlPath = "webparts/Language/LanguageManager.ascx";
            }
            else if (type.Equals("ser", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 4;
                controlPath = "webparts/URLSecurity/URLSecurityManager.ascx";
            }
            else if (type.Equals("page", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 1;
                controlPath = "webparts/Plug-inForwarder/PlugInForwarder.ascx";
            }
            else if (type.Equals("upload", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 2;
                controlPath = "webparts/Common/FileUploader.ascx";
            }
            else if (type.Equals("profile", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 0;
                controlPath = "webparts/Common/ProfileManagement.ascx";
            }
            else if (type.Equals("plug_in", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 6;
                controlPath = "webparts/Plug-in/PlugInManager.ascx";
            }
            else if (type.Equals("invalidurl", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = -1;
                controlPath = "webparts/Common/InvalidURL.ascx";
            }
            else if (type.Equals("site", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 0;
                controlPath = "webparts/Common/SiteSwitch.ascx";
            }
            else if (type.Equals("siteedit", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 0;
                controlPath = "webparts/Common/SiteInput.ascx";
            }
            else if (type.Equals("siteadd", StringComparison.OrdinalIgnoreCase))
            {
                _TopMenuItem = 0;
                controlPath = "webparts/Common/SiteInput.ascx";
            }
        }
        else
        {
            _TopMenuItem = 0;
            controlPath = "webparts/Common/HomePage.ascx";
        }

        if (controlPath != string.Empty)
        {
            try
            {
                Control ctrl = LoadControl(controlPath);
                this.main.Controls.Add(ctrl);
            }
            catch (Exception)
            { }
        }
    }

    protected bool CheckAuthentication()
    {
        string type = Request.QueryString["type"];
        type = type == null ? "" : type;

        if (type.Equals("lang", StringComparison.OrdinalIgnoreCase)
            || type.Equals("plug_in", StringComparison.OrdinalIgnoreCase))
        {
            if (!Context.User.Identity.Name.Equals("admin", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
        }
        else if (type.Equals("user", StringComparison.OrdinalIgnoreCase) 
            || type.Equals("ser", StringComparison.OrdinalIgnoreCase))
        {
            AdminUser loggedUser;
            try
            {
                loggedUser = new AdminUser(Context.User.Identity.Name);
                if (loggedUser.ID < 1 || loggedUser.Type != AdminUser.ADMIN_TYPE)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        else if (!type.Equals(""))
        {
            AdminUser loggedUser;
            try
            {
                loggedUser = new AdminUser(Context.User.Identity.Name);
                if (loggedUser.ID < 1)
                {
                    throw new Exception();
                }
                else if (loggedUser.Type == AdminUser.ADMIN_TYPE)
                {
                    return true;
                }
                else
                {
                    String url = Request.Url.AbsoluteUri;
                    if (url.Contains("?"))
                    {
                        url = url.Substring(url.LastIndexOf("?"));
                    }

                    return URLMap.CheckAuthentication(url, loggedUser.ID);
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        return true;
    }
}
